//
//  OtherViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "OtherViewController.h"
#import "GetDataMethods.h"
#import "UIImageView+WebCache.h"
#import "OtherTableViewCell.h"

#import "PictureViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "Model.h"
#import "Fit.h"
#import "MBProgressHUD.h"


@interface OtherViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSArray *typeArray;
@property(nonatomic, retain)NSArray *tuArray;
@property(nonatomic, retain)NSString *string;
@property(nonatomic, assign)NSInteger number;
@property(nonatomic, retain)NSString *url;
@property(nonatomic, retain)NSString *imageString;
@property(nonatomic, retain)MBProgressHUD *progressView;

@end

@implementation OtherViewController
-(void)dealloc
{
    [_tableView release];
    [_typeArray release];
    [_tuArray release];
    [super dealloc];
}

-(void)changeColor{
    self.tableView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = _data.name;
    [self changeColor];
}
-(void)getData
{
    [GetDataMethods getDataByString:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/", _data.type, _data.Id] BodyString:nil WithDataBlock:^(id data) {
        self.string = [data objectForKey:@"id"];
        self.number = [[data objectForKey:@"type"]integerValue];
        self.url = [data objectForKey:@"url"];
        self.imageString = [[data objectForKey:@"hottest_places"][0]objectForKey:@"photo"];
        [_tableView reloadData];
        [self createViews];
        [_progressView removeFromSuperview];
    }];
}

-(void)loadView
{
    [super loadView];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    [self getData];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor colorWithRed:1 green:0.95 blue:0.85 alpha:1];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    self.typeArray = [NSArray arrayWithObjects:@"精品游记", @"实用须知", @"旅行地点", @"不可错过", @"主题榜单", @"推荐路线", nil];
    self.tuArray = [NSArray arrayWithObjects:@"11.png", @"33.png", @"44.png", @"55.png", @"66.png", @"77.png", nil];

    //初始化加载中视图
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"玩命加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
    [self.view addSubview:_progressView];
    [_progressView release];
    
}
-(void)createViews
{
    UIImageView *topImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250 * H)];
    [topImageView sd_setImageWithURL:[NSURL URLWithString:_imageString]];
    _tableView.tableHeaderView = topImageView;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    [topImageView addGestureRecognizer:tap];
    topImageView.userInteractionEnabled = YES;
    [tap release];
    [topImageView release];
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 * W, 200 * H, topImageView.frame.size.width - 40 * W, 40 * H)];
    nameLabel.text = _data.name;
    nameLabel.font = [UIFont systemFontOfSize:30];
    nameLabel.textColor = [UIColor whiteColor];
    [topImageView addSubview:nameLabel];
    [nameLabel release];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    OtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[OtherTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.nameLabel.text = _typeArray[indexPath.row];
    cell.iconImageView.image = [UIImage imageNamed:_tuArray [indexPath.row]];
    cell.backgroundColor = [Model shareInstance].BackgroundColor;
    cell.nameLabel.textColor = [Model shareInstance].TitleColor;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _typeArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 * H;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        SecondViewController *secVC = [[SecondViewController alloc]init];
        secVC.string = _url;
        [self.navigationController pushViewController:secVC animated:YES];
    }
    else if (indexPath.row == 1) {
        ThirdViewController *thiVC = [[ThirdViewController alloc]init];
        thiVC.url = _url;
        NSString *string = @"overview";
        thiVC.string = string;
        [self.navigationController pushViewController:thiVC animated:YES];

    }
    else if (indexPath.row == 2) {
//        ThirdViewController *thiVC = [[ThirdViewController alloc]init];
//        thiVC.url = _url;
//        NSString *string = @"experience";
//        thiVC.string = string;
//        [self.navigationController pushViewController:thiVC animated:YES];
        
        FirstViewController *firVC = [[FirstViewController alloc]init];
        firVC.IDString = _string;
        firVC.num = _number;
        [self.navigationController pushViewController:firVC animated:YES];
        
    }
    else if (indexPath.row == 3) {
        ThirdViewController *thiVC = [[ThirdViewController alloc]init];
        thiVC.url = _url;
        NSString *string = @"intro";
        thiVC.string = string;
        [self.navigationController pushViewController:thiVC animated:YES];
    }
    else if (indexPath.row == 4) {
        ThirdViewController *thiVC = [[ThirdViewController alloc]init];
        thiVC.url = _url;
        NSString *string = @"top10_list";
        thiVC.string = string;
        [self.navigationController pushViewController:thiVC animated:YES];
    }
    else if (indexPath.row == 5) {
        ThirdViewController *thiVC = [[ThirdViewController alloc]init];
        thiVC.url = _url;
        NSString *string = @"routes";
        thiVC.string = string;
        [self.navigationController pushViewController:thiVC animated:YES];
    }
    
}
-(void)tapAction
{
    PictureViewController *picVC = [[PictureViewController alloc]init];
    picVC.string = _string;
    picVC.number = _number;
    [self.navigationController pushViewController:picVC animated:YES];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
